/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			prpex_color.cc
  \date			june 2011
  \author		TNick

  \brief		string class


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read ReadMe.txt and Licence.txt in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QLineEdit>
#include	<QColorDialog>

#include	"prpmdl.h"
#include	"prpview.h"
#include	"prpex_color.h"
#include	"prpex_integer.h"


/*  INCLUDES    ============================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
PEX_Col::PEX_Col		(
		PrpMdl* pa_mdl, PrpNode* pa_fth, QString stra_name,
		QString stra_descr, QRgb init_rgb__col) :
		PrpNode(pa_mdl, pa_fth, PrpTpl::COLOR, stra_name, stra_descr)
{
	rgb_value = init_rgb__col;
	iconDec(true);

	PEX_Int*	i_kid;
	i_kid = new PEX_Int(
			model(),
			static_cast<PrpNode*>(this),
			QObject::tr("Red"),
			QObject::tr("red component of the color"),
			qRed(rgb_value));
	i_kid->setLimits(0,255);
	i_kid->inferior(true);
	i_kid = new PEX_Int(
			model(),
			static_cast<PrpNode*>(this),
			QObject::tr("Green"),
			QObject::tr("green component of the color"),
			qGreen(rgb_value));
	i_kid->setLimits(0,255);
	i_kid->inferior(true);
	i_kid = new PEX_Int(
			model(),
			static_cast<PrpNode*>(this),
			QObject::tr("Blue"),
			QObject::tr("blue component of the color"),
			qBlue(rgb_value));
	i_kid->setLimits(0,255);
	i_kid->inferior(true);

}

/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QWidget*	PEX_Col::editCreate	(QWidget *parent, const QStyleOptionViewItem &option)
{
	Q_UNUSED(option);
	QLineEdit *editor = new QLineEdit(parent);
	return editor;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PEX_Col::editLoad	(QWidget *editor)
{
	QLineEdit *lne = static_cast<QLineEdit*>(editor);
	QString toshow = valAsText().toString();
	lne->setText(toshow);
}
/* ======================================================================== */

/* ------------------------------------------------------------------------- */
void		PEX_Col::editSave	(QWidget *editor)
{
	QLineEdit *lne = static_cast<QLineEdit*>(editor);
	QString str_value = lne->text();
	if (str_value.isEmpty())
		return;

	/* allow standard Qt naming */
	if (QColor::isValidColor(str_value))
		{
			QColor qc_temp = QColor();
			qc_temp.setNamedColor(str_value);
			if (qc_temp.isValid())
				{
					rgb_value = qc_temp.rgb();
					updInferiors();
					informParent();
					return;
				}
		}


	/* text parsing for a color */
	/* parantesis are allowed in any form or may be missing */

	int iCrt = 0;
	int iEnd;
	int val[3];
	int	i = 0;
	bool b_conv;

	/* skip white spaces and starting brackets */
	for (;;)
		{
			if (iCrt >= str_value.length())
				return;
			if (str_value[iCrt] == '(')
				iCrt += 1;
			else if (str_value[iCrt] == '[')
				iCrt += 1;
			else if (str_value[iCrt] == '{')
				iCrt += 1;
			else if (str_value[iCrt] == ' ')
				iCrt += 1;
			else if (str_value[iCrt] == 9)
				iCrt += 1;
			else
				break;
		}


	for (;;)
		{
			iEnd = iCrt;

			/* either number or failure */
			for (;;)
				{
					if (iEnd >= str_value.length())
						break;
					if (!str_value[iEnd].isNumber())
						break;
					iEnd++;
				}
			if (iEnd == iCrt)
				return; /* number expected */
			val[i] = str_value.mid(iCrt, iEnd-iCrt).toInt(&b_conv);
			if (b_conv == false)
				return;
			if ((val[i] < 0) || (val[i] > 255))
				return;
			if (i == 2) break;
			i++;

			/* skip separators and white spaces */
			iCrt = iEnd;
			for (;;)
				{
					if (iCrt >= str_value.length())
						return;
					else if (str_value[iCrt] == ',')
						iCrt++;
					else if (str_value[iCrt] == ' ')
						iCrt++;
					else if (str_value[iCrt] == 9)
						iCrt++;
					else
						break;
				}

		}


	rgb_value = qRgb(val[0], val[1], val[2]);
	updInferiors();
	informParent();
	return;
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
bool		PEX_Col::changeVal	(QVariant new_value)
{


	return false;
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void		PEX_Col::editIcon	(PrpView * prp_v, QModelIndex the_idx)
{
	QColor cl_choice = QColorDialog::getColor(
			QColor(rgb_value),
			(QWidget*)prp_v,
			QObject::tr("Choose color..."),
			QColorDialog::ShowAlphaChannel);
	if (cl_choice.isValid())
		{
			PrpMdl* mdl = static_cast<PrpMdl*>(prp_v->model());
			rgb_value = cl_choice.rgba();
			updInferiors();
			informParent();
			mdl->dataHasChanged(the_idx);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void		PEX_Col::updInferiors (void)
{
	PrpMdl* mdl = static_cast<PrpMdl*>(model());
	if (hd.count() != 3)
		{
			Q_ASSERT(0);
			return;
		}

	PEX_Int*	i_kid_1 = static_cast<PEX_Int*>(hd.first());
	i_kid_1->setValue(qRed(rgb_value));

	PEX_Int*	i_kid_2 = static_cast<PEX_Int*>(i_kid_1->next());
	i_kid_2->setValue(qGreen(rgb_value));

	PEX_Int*	i_kid_3 = static_cast<PEX_Int*>(i_kid_2->next());
	i_kid_3->setValue(qBlue(rgb_value));

	mdl->dataHasChanged(
			mdl->createIndex(0,0,i_kid_1),
			mdl->createIndex(2,0,i_kid_3)
			);
}
/* ========================================================================= */


/* ------------------------------------------------------------------------- */
void		PEX_Col::subChanged	(PrpTpl* hot_item)
{

	PrpMdl* mdl = static_cast<PrpMdl*>(model());
	if (hd.count() != 3)
		{
			Q_ASSERT(0);
			return;
		}
	PEX_Int*	i_kid = static_cast<PEX_Int*>(hot_item);

	int iK = hot_item->index(&hd);
	switch (iK)
		{
		case	0:		{
						rgb_value = qRgb(
								i_kid->value(),
								qGreen(rgb_value),
								qBlue(rgb_value));
						break;}
		case	1:		{
						rgb_value =
								qRgb(qRed(rgb_value),
									 i_kid->value(),
									 qBlue(rgb_value));
						break;}
		default:/*2*/	{
					rgb_value =
							qRgb(qRed(rgb_value),
								 qGreen(rgb_value),
								 i_kid->value());
					break;}
		}
	informParent();
	mdl->dataHasChanged(
			mdl->createIndex(row(),0,this)
			);

}
/* ========================================================================= */



/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
